﻿#region

using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using InfoTech.VinhSon.Bussiness;
using VinhSon.Models;

#endregion

namespace VinhSon
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                            "Home", // Route name
                            "Trang-chu", // URL with parameters
                            new
                            {
                                controller = "Home",
                                action = "Home",
                            } // Parameter defaults
                    );
            routes.MapRoute(
                            "Article", // Route name
                            "{article}-{id}a", // URL with parameters
                            new
                                {
                                    controller = "PageArticle",
                                    action = "Index",
                                    id = UrlParameter.Optional,
                                    category = UrlParameter.Optional,
                                    categoryId = UrlParameter.Optional,
                                    article = UrlParameter.Optional
                                } // Parameter defaults
                    );
            routes.MapRoute(
                            "Category", // Route name
                            "{category}-{id}c", // URL with parameters
                            new
                                {
                                    controller = "PageCategory",
                                    action = "Index",
                                    id = UrlParameter.Optional,
                                    category = UrlParameter.Optional
                                } // Parameter defaults
                    );
            routes.MapRoute(
                            "Default", // Route name
                            "{controller}/{action}/{id}", // URL with parameters
                            new
                                {
                                    controller = "Home",
                                    action = "Index",
                                    id = UrlParameter.Optional
                                } // Parameter defaults
                    );
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            // Use LocalDB for Entity Framework by default
            Database.DefaultConnectionFactory = new SqlConnectionFactory(@"Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True");
            Database.SetInitializer<VinhSonContext>(null);
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

        protected void Session_Start()
        {
            if (User.Identity.IsAuthenticated)
            {
                string userName = User.Identity.Name;
                if (!string.IsNullOrEmpty(userName))
                {
                    UnitOfWork unitOfWork = new UnitOfWork();
                    User user = unitOfWork.UserRepository.Select(c => c.Username == userName)
                                          .FirstOrDefault();
                    if (user != null)
                        if (false == user.Inactive || null == user.Inactive)
                            if (true == user.Activated)
                            {
                                Session["UserId"] = user.Id;
                                FormsAuthentication.SetAuthCookie(user.Username,
                                                                  true);
                            }
                            else FormsAuthentication.SignOut();
                        else FormsAuthentication.SignOut();
                    else FormsAuthentication.SignOut();
                }
            }
        }

    }
}
